﻿//======================================================================
//  MD5加密
//====================================================================== 

using System;
using System.Collections.Generic;

using System.Text;
using System.Security.Cryptography;
using System.Web.Security;

namespace Common
{
    /// <summary>
    /// MD5加密
    /// </summary>
    public sealed class MD5
    {
        /// <summary>
        /// MD5加密密匙
        /// </summary>
        public static readonly string MD5SECRET = "L82V6ZVD6JMD5";

        /// <summary>
        /// 对字节数据进行MD5加密
        /// </summary>
        /// <param name="data">需加密的字节数组</param>
        /// <returns>加密后的字符串</returns>
        public static string MD5ToHexString(byte[] data)
        {
            System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string t = "";
            string tTemp = "";
            for (int i = 0; i < result.Length; i++)
            {
                tTemp = Convert.ToString(result[i], 16);
                if (tTemp.Length != 2)
                {
                    switch (tTemp.Length)
                    {
                        case 0: tTemp = "00"; break;
                        case 1: tTemp = "0" + tTemp; break;
                        default: tTemp = tTemp.Substring(0, 2); break;
                    }
                }
                t += tTemp;
            }
            return t;
        }
        /// <summary>
        /// 对字符串进行MD5加密
        /// </summary>
        /// <param name="strText">需加密的字符串</param>
        /// <returns>加密后字符串</returns>
        public static string MD5ToHexString(string strText)
        {
            byte[] data = System.Text.ASCIIEncoding.Unicode.GetBytes(strText);
            System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string t = "";
            for (int i = 0; i < result.Length; i++)
            {
                t += Convert.ToString(result[i], 16);
            }
            return t;
        }
        /// <summary>
        /// 对字符串进行MD5加密
        /// </summary>
        /// <param name="strText">需加密的字符串</param>
        /// <param name="encode">编码方式</param>
        /// <returns>加密后字符串</returns>
        public static string MD5ToHexString(string strText,Encoding encode)
        {
            System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(encode.GetBytes(strText));
            StringBuilder strbText = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                strbText.Append(result[i].ToString("x2"));
            }
            return strbText.ToString();
        }
        /// <summary>
        /// MD5函数，DiscuzNt论坛使用的加密方式
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public static string MD5ForDiscuz(string str)
        {
            byte[] b = Encoding.UTF8.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
                ret += b[i].ToString("x").PadLeft(2, '0');

            return ret;
        }
        #region 将取输入的字符串的md5值
        /// <summary>
        /// 将取输入的字符串的md5值
        /// </summary>
        /// <param name="inputstr">输入的字符串</param>
        /// <param name="otherstr">参与运算的字符</param>
        /// <returns></returns>
        public static string StringToMd5(string inputstr, string otherstr)
        {
            if (!string.IsNullOrEmpty(inputstr))
            {
                string value = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(inputstr + otherstr, "md5");
                if (!string.IsNullOrEmpty(value))
                    return value;
                else
                    return null;
            }
            else
                return null;
        }
        #endregion
    }
}
